/**
 * File: ConnecctionBD.java
 * Data: 03.10.2010
 * Objective: Criar uma classe para gerir a conexÃ£o com a Base de Dados.
 *
 * Copyright 2010 S2DZ CORPORATION

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Paulo Ilenga
 *
 * Classe que permite abrir  fechar a conexao com a BD.
 */
public abstract class ConnecctionBD
{
    protected Connection connection;
    private final String DRIVER = "org.postgresql.Driver";
    private final String USER = "postgres";
    private final String PASSWORD = "root";
    private final String URL = "jdbc:postgresql://localhost:5432/gcboutique";

    /**
     * Permite abrir uma conexao com a Base de Dados.
     */
    protected void connect()
    {
        try
        {
            Class.forName(DRIVER);

            connection = DriverManager.getConnection(URL, USER, PASSWORD);
        }
        catch (Exception ex)
        {
            Logger.getLogger(ConnecctionBD.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Permite fechar uma conexÃ£o com a Base de Dados
     */
    protected void disconnect()
    {
        try
        {
            connection.close();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(ConnecctionBD.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Permite retornar a conexÃ£o estabelecida com a BD.
     * @return a conexão com a BD.
     */
    public Connection getConnection()
    {
        return connection;
    }
}
